<script lang="tsx">
/**
 * 项目实施管理
 */
export default {
  name: "develop-project-implement"
}
</script>

<script setup lang="tsx">



import {ref} from "vue";
import {convertEnumToList, definePageModel, PageModel,processRequest} from "@/framework";
import ImplementDetail from "@/pages/develop-project/develop-project-implement/components/implement-detail/index.vue";
import APIS from "@/apis";
import {ElMessage, ElMessageBox} from "element-plus";
import {ImplementStatusEnum} from "@/pages/develop-project/develop-project-implement/index.ts";

const pageModelRef = ref()
const config = definePageModel<any>({
  url:APIS.DEVELOP_PROJECT_IMPLEMENT_BASE,
  searchForm:{
    beforeSubmit(data) {
      if (data.endTime) {
        data.endTime += " 23:59:59"
      }
      return data
    },
    els:[
      {
        eType:"el-input",
        prop:"project",
        props:{
          placeholder:"项目编码/名称",
          clearable:true
        }
      },
      {
        eType: "el-select",
        prop: "implementStatus",
        props: {
          placeholder: "实施状态",
          clearable: true
        },
        optionsData: {
          list: convertEnumToList(ImplementStatusEnum),
          label: "label",
          value: "value"
        }
      },
      {
        eType: "el-date-picker",
        prop: "startTime",
        props: {
          valueFormat: "YYYY-MM-DD",
          placeholder: "创建时间-起始",
          clearable: true
        }
      },
      {
        eType: "el-date-picker",
        prop: "endTime",
        props: {
          valueFormat: "YYYY-MM-DD",
          placeholder: "创建时间-结束",
          clearable: true
        }
      },
    ]
  },
  table:{
    page:{
      enable:true,
      pageSize:20
    },
    selection:true,
    operation:{
      width:120,
      props:{
        fixed:"right"
      },
      els:[
        {
          eType: "el-button",
          label: "详情",
          event: "detail",
          props: {
            type: "primary",
            link: true
          }
        },
        {
          eType: "el-button",
          label: "开始",
          event: "start",
          props: {
            type: "primary",
            link: true
          }
        },
        {
          eType: "el-button",
          label: "完成",
          event: "finish",
          props: {
            type: "primary",
            link: true
          }
        },
      ]
    },
    props:{
      stripe:true,
      border:true
    },
    els:[
      {
        label:"项目编码",
        prop:"projectCode",
        minWidth:120,
      },
      {
        label:"项目名称",
        prop:"projectName",
        minWidth:120,
      },
      {
        label:"开发人员姓名",
        prop:"developStaffName",
        minWidth:120,
      },
      {
        label:"实施状态",
        prop:"status",
        minWidth:120,
        renderFn(data) {
          return <span>{ImplementStatusEnum[data.implementStatus]}</span>
        },
      },
      {
        label: "备注",
        prop: "remark",
        minWidth: 120
      },
      {
        label: "创建人姓名",
        prop: "createStaffName",
        minWidth: 120
      },

      {
        label: "创建时间",
        prop: "createTime",
        minWidth: 140,
        renderFn(data) {
          return <span>{data.createTime?.toDate()}</span>
        },
      },
    ]
  },
})

/**
 * 刷新
 */
const handleRefresh = (clear: boolean = false) => {
  pageModelRef.value.refreshTableData()
  clear && pageModelRef.value.clearSelection()
}

/**
 * 详情
 */
const implementDetailRef = ref()
const handleDetail = (row: any) => {
  implementDetailRef.value.show({
    id: row.projectId
  })
}


/**
 * 开始 - 单选
 */
const handleStart = async (row: any) => {
  if (row.implementStatus !== ImplementStatusEnum['待反馈']) {
    ElMessage.warning("只有待反馈的项目才可以开始");
    return false;
  }
  await ElMessageBox.confirm("是否确认开始实施?", "确认", {
    type: "warning"
  });
  await processRequest.post('/api/DevelopProjectImplement/Start', {
    id:row.id
  });
  ElMessage.success("状态修改成功");
  handleRefresh();
}

/**
 * 完成 - 单选
 */
const handleFinish = async (row: any) => {
  if (row.implementStatus !== ImplementStatusEnum['待实施']) {
    ElMessage.warning("只有待实施的项目才可以结束");
    return false;
  }
  await ElMessageBox.confirm("是否确认结束实施?", "确认", {
    type: "warning"
  });
  await processRequest.post('/api/DevelopProjectImplement/Finish', {
    id: row.id
  });
  ElMessage.success("状态修改成功");
  handleRefresh();
}


</script>

<template>
  <div class="develop-project">
    <PageModel ref="pageModelRef" :PageModel="config" @detail="handleDetail" @start="handleStart" @finish="handleFinish"/>
    <!-- 详情 -->
    <ImplementDetail ref="implementDetailRef"/>
  </div>
</template>

<style scoped lang="scss">
.develop-project {
  flex: 1;
  display: flex;
  flex-direction: column;
}
</style>